package org.ruanjj.twonumbersadded;

import org.ruanjj.linkedlist.ListNode;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode cur = result;
        int count = 0;
        while (null != l1 || null != l2 || count != 0) {
            int sum = 0;
            if (null != l1) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (null != l2) {
                sum += l2.val;
                l2 = l2.next;
            }
            sum += count;
            count = sum >= 10 ? 1 : 0;
            sum = sum % 10;
            cur.next = new ListNode(sum);
            cur = cur.next;
        }
        return result.next;
    }
}
